import { setLoginedUser, getLoginedUser, removeLoginedUser } from "@/plugins/utils/auth";
import _ from "underscore"
let loginedUser = getLoginedUser()
let loginedUser2 = getLoginedUser(2)
let loginedUser3 = getLoginedUser(3)

const user = {
    state: {
        info: loginedUser,
        info2: loginedUser2,
        info3: loginedUser3,
        userClient: "",
        logined: false,
        logining: false,

        sysParam: {}, //系统常量

        menuActive: 0
    },

    mutations: {
        SET_USER: (state, user) => {
            var type = user.type
            if(type==2){
                if(user.loginType==1){
                    state.info2 = user
                } else {
                    state.info3 = user
                }
            } else {
                state.info = user
            }
        },
        SET_CLIENT: (state, client) => {
            console.log(client)
            state.userClient = client
        },
        SET_LOGINED: (state, client) => {
            state.logined = true
        },
        SET_LOGININD: (state, status) => {
            state.logining = status
        },
        SET_SYS_PARAM: (state, sysParam) => {
            state.sysParam = sysParam || {}
        },

        SET_MENU: (state, active) => {
            state.menuActive = active
        },
    },

    actions: {
        SetMenuActive({ commit, state }, active) {
            commit('SET_MENU', active)
        },
        SetUserLogin({ commit, state }, client) {
            commit('SET_LOGINED', client)
        },
        SetUserLogining({ commit, state }, status) {
            commit('SET_LOGININD', status)
        },
        SetUserClient({ commit, state }, client) {
            commit('SET_CLIENT', client)
        },
        SetLoginUser({ commit, state }, user) {
            var type = user.type
            if(type==2){
                if(user.loginType==1){
                    user = _.assign({}, getLoginedUser(2), user)
                    setLoginedUser(user, 2)
                } else {
                    user = _.assign({}, getLoginedUser(3), user)
                    setLoginedUser(user, 3)
                }
                
                commit('SET_USER', user)
            } else {
                user = _.assign({}, getLoginedUser(), user)
                setLoginedUser(user)
                commit('SET_USER', user)
            }
        },
        SetUserMobile({ commit, state }, mobile, type) {
            if(type==2){
                if(user.loginType==1){
                    user = _.assign({}, getLoginedUser(2), {mobile: mobile})
                    setLoginedUser(user, 2)
                } else {
                    user = _.assign({}, getLoginedUser(3), {mobile: mobile})
                    setLoginedUser(user, 3)
                }
                commit('SET_USER', user)
            } else {
                var user = _.assign({}, getLoginedUser(), {mobile: mobile})
                setLoginedUser(user)
                commit('SET_USER', user)
            }
        },
        // 登出
        LogOut({ commit, state }, type) {
            if(type==2||type==3){
                return new Promise((resolve, reject) => {
                    removeLoginedUser(type)
                    commit('SET_USER', {type: type})
                    resolve();
                });
            } else {
                return new Promise((resolve, reject) => {
                    removeLoginedUser()
                    commit('SET_USER', {})
                    resolve();
                });
            }
        },

        SetSysParam({ commit, state }, sysParam) {
            commit('SET_SYS_PARAM', sysParam)
        },
    }
};

export default user;
